﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBHelper;
using System.Data.SqlClient;
using System.Data;
using BusinessLayer.WorldArtPrice.Entities;
using BusinessLayer.WorldArtPrice.Models;

namespace BusinessLayer.WorldArtPrice
{
    public class ArtWorkBL
    {
        private readonly DbHelper _db;

        public ArtWorkBL()
        {
            _db = new DbHelper(new DbConfig().DbWorldArtPriceConnectionString);         
        }

        public List<ArtWork> GetByPriceId(int priceId)
        {
            var oCommand = new SqlCommand("sp_ArtWork_Select_By_PriceId") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@PriceId", priceId));            

            var list = _db.GetList<ArtWork>(oCommand);
            return list;
        }
        
        public ArtWork GetById(int id)
        {
            var oCommand = new SqlCommand("sp_ArtWork_Select_By_Id") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Id", id));            

            var list = _db.GetList<ArtWork>(oCommand);
            return list.FirstOrDefault();
        }

        public List<ArtWorkModel> GetByItemType(int itemType, int pageIndex, int pageSize, int sort)
        {
            var oCommand = new SqlCommand("sp_ArtWork_Select_By_ItemType") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@ItemType", itemType));
            oCommand.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            oCommand.Parameters.Add(new SqlParameter("@PageSize", pageSize));
            oCommand.Parameters.Add(new SqlParameter("@Sort", sort));

            var list = _db.GetList<ArtWorkModel>(oCommand);
            return list;
        }
        
        public void GetCountByItemType(int itemType, out int totalRecord)
        {
            var oCommand = new SqlCommand("sp_ArtWork_Select_Count_By_ItemType") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@ItemType", itemType));
            var output = new SqlParameter("@TotalRecord", SqlDbType.Int) { Direction = ParameterDirection.Output };
            oCommand.Parameters.Add(output);
            _db.ExecuteNonQuery(oCommand);
            totalRecord = (int)output.Value;
        }

        public void Insert(ArtWork artWork)
        {
            var oCommand = new SqlCommand("sp_ArtWork_Insert") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@PriceId", artWork.PriceId));
            oCommand.Parameters.Add(new SqlParameter("@ArtworkFile", artWork.ArtworkFile));
            oCommand.Parameters.Add(new SqlParameter("@DateCreated", artWork.DateCreated));            

            _db.ExecuteNonQuery(oCommand);

        }

        public void DeleteByPriceId(int priceId)
        {
            var oCommand = new SqlCommand("sp_ArtWork_Delete_By_PriceId") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@PriceId", priceId));

            _db.ExecuteNonQuery(oCommand);
        }
    }
}
